#! -*- coding:utf-8 -*-
import requests
import re
import os
import hashlib
import sys
from urllib import request
import time


BASE_DIR = os.path.dirname(__file__)

def Schedule(blocknum, blocksize, totalsize):
    speed = (blocknum * blocksize) / (time.time() - start_time)
    speed_str = " Speed: {:.2f} KB".format(float(speed) / 1024)
    recv_size = blocknum * blocksize
    # config scheduler
    f = sys.stdout
    pervent = recv_size / totalsize
    if pervent > 1:
        pervent = 1
    percent_str = "{:.2f}%".format(pervent * 100)
    n = round(pervent * 50)
    s = ('=' * n).ljust(50, '-')
    f.write('\r' + percent_str.ljust(4, ' ') + '[' + s + ']' + speed_str)
    f.flush()


def CalcFileSha256(filname):
    ''' calculate file sha256 '''
    with open(filname, "rb") as f:
        sha256obj = hashlib.sha256()
        sha256obj.update(f.read())
        hash_value = sha256obj.hexdigest()
        return hash_value


def download(url, sha256='', dirs = 'pkgs'):
    pkgs_dir = os.path.join(BASE_DIR, dirs)
    if not os.path.exists(pkgs_dir):
        os.makedirs(pkgs_dir)
    file_name = os.path.join(pkgs_dir, url.split('/')[-1])
    if os.path.exists(file_name):
        print('{}'.format(file_name).ljust(70), 'exists.')
        tmp = CalcFileSha256(file_name)
        if tmp == sha256:
            print('verify passed')
            return True
        else:
            print('verify failed.')
    print('downloading {}'.format(url))
    global start_time
    start_time = time.time()
    request.urlretrieve(url, file_name, Schedule)
    print('\n')
    tmp = CalcFileSha256(file_name)
    if tmp == sha256:
        return True
    return False


def other_download(file_name):
    with open(file_name, 'r', encoding='utf-8')as f:
        lines = f.readlines()
    suc, fail = 0, 0
    for line in lines:
        line = line.strip().split(' ')
        if len(line) < 2:
            print(line, 'parse error')
            continue
        url, sha256 = line[0], line[1]
        if download(url, sha256, './'):
            suc += 1
        else:
            fail += 1
    return suc, fail


if __name__ == '__main__':
    file = 'pkgs.txt'
    with open(file, 'r')as f:
        data = f.readlines()
    pkgs = []
    name = ''
    sha256 = ''
    urls = []
    for line in data:
        if 'name =' in line:
            name = line.split('=')[1].strip()
        if 'sha256 = ' in line:
            sha256 = line.split('=')[1].strip()
        if 'https://' in line or 'http://' in line:
            urls.append(line.strip()[1:-2])
        if line.strip() == '':
            if name != '' and sha256 != '' and len(urls)>0:
                tmp = {
                    'name': name[1:-2],
                    'sha256': sha256[1:-2],
                    'urls': urls
                }
                pkgs.append(tmp)
            name = ''
            sha256 = ''
            urls = []

    len_pkgs = len(pkgs)
    success = 0
    failed = 0
    for i in pkgs:
        sha256 = i['sha256']
        urls = i['urls']
        for url in urls:
            try:
                start_time = time.time()
                tmp = download(url, sha256)
            except Exception as e:
                print(e)
                tmp = False
        if tmp:
            success += 1
        else:
            failed += 1
    suc, fail = other_download('other_pkgs.txt')
    success += suc
    failed += fail
    len_pkgs += suc + fail
    print('Total pkgs:{} success:{} failed:{}'.format(len_pkgs, success, failed))